//
//  HomeScrollView.m
//  网易Header
//
//  Created by 何欣 on 15/7/2.
//  Copyright (c) 2015年 何欣. All rights reserved.
//

#import "HomeTableViewHeader.h"
#import "HomeCollectionViewCell.h"

#define DescLabelHeight  20

@interface HomeTableViewHeader ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic,strong)UICollectionView *homeCollectionView;

@property(nonatomic,strong)UILabel *descLabel;
@property(nonatomic,strong)UIPageControl *pageControl;
@property(nonatomic,strong)NSMutableArray *dataSource;
@property(nonatomic,strong)NSTimer *timer;

@end

@implementation HomeTableViewHeader
static NSInteger count = 0;

-(instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, frame.size.height - DescLabelHeight);
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _homeCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0,frame.size.width, frame.size.height - DescLabelHeight) collectionViewLayout:flowLayout];
        _homeCollectionView.pagingEnabled = YES;
        _homeCollectionView.scrollEnabled = YES;
        _homeCollectionView.delegate   = self;
        _homeCollectionView.dataSource = self;
        [self addSubview:_homeCollectionView];
        [_homeCollectionView registerClass:[HomeTableViewHeaderCell class] forCellWithReuseIdentifier:@"CollectionViewCell"];
        
        _descLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, _homeCollectionView.frame.origin.x + _homeCollectionView.frame.size.height + 5, 200 , DescLabelHeight - 5)];
        _descLabel.text = @"这是一段测试的描述";
        _descLabel.font = [UIFont systemFontOfSize:15.0f];
        [self addSubview:_descLabel];
    
        _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(KScreenWidth - 100, _descLabel.frame.origin.y, 100, DescLabelHeight)];
        _pageControl.currentPage = 0;
        _pageControl.pageIndicatorTintColor = [UIColor blackColor];
        _pageControl.currentPageIndicatorTintColor = [UIColor redColor];
        [self addSubview:_pageControl];
        
    }
    return self;
    
}


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1000;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _dataSource.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HomeTableViewHeaderCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];
    [cell setImageForImageView:[_dataSource[indexPath.row] descImg]];
    _descLabel.text = [_dataSource[indexPath.row] title];
    
    return cell;
}


/**
 *  手指滑动时，count加一 防止与timer的响应时出现滚回去的情况
 *
 *  @param scrollView 
 */

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    count++;
    _pageControl.currentPage =  count % _dataSource.count;

}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    _pageControl.currentPage =  count % _dataSource.count;
    
}

#pragma mark-

/**
 *  给header赋值，如果数组个数为1 则不创建定时器，pageControl隐藏
 *
 *  @param array 在HomeCollectionViewCell中解析，获取的轮播图的数据
 */
-(void)setHeaderData:(NSMutableArray *)array{
    
    _dataSource = [NSMutableArray arrayWithArray:array];

    if (array.count > 1) {
        _timer = nil;
        [_timer invalidate];
        _timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];
        _pageControl.hidden = NO;
    }else{
        _pageControl.hidden = YES;
    }
    _pageControl.numberOfPages = _dataSource.count;
    [_homeCollectionView reloadData];
}


#pragma mark- 自播放动
/**
 *  自播放动  timer响应方法
 */
-(void)timeAction{
    
    count ++;
    NSInteger item = count  % _dataSource.count;
    NSInteger section = count / _dataSource.count;
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:item inSection:section];
    
    [_homeCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
